package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {
    //根据课程id查找章节和课程信息

    public void findSectionAndLessonByCourseId(HttpServletRequest req, HttpServletResponse resp){
        try {
            //1.获取参数
            String course_id = req.getParameter("course_id");

            //2.业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            List<Course_Section> sectionList = contentService.findSectionAndLessonByCourseId(Integer.parseInt(course_id));
            //3.返回响应结果
            String result = JSON.toJSONString(sectionList);
            resp.getWriter().println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //回显课程信息

    public void findCourseById(HttpServletRequest req,HttpServletResponse resp){
        try {
            //1.获取参数
            String courseId = req.getParameter("course_id");

            //2.业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            Course course = contentService.findCourseById(Integer.parseInt(courseId));

            //3.返回数据，将需要回显的字段转换为Json格式
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name");
            String result = JSON.toJSONString(course,filter);
            resp.getWriter().println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //新增或修改章节信息
    public void saveOrUpdateSection(HttpServletRequest req,HttpServletResponse resp){
        try {
            //1.获取Dao接口设置的域对象中的参数
            Map<String,Object> map = (Map) req.getAttribute("map");
            //2.创建Course_Section
            Course_Section section = new Course_Section();
            //3.使用BeabUtils工具类，将map中的数据封装到section中
            BeanUtils.populate(section,map);
            //4.业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            //判断是修改或是新增章节信息
            String result;
            if(section.getId() != 0){
                //修改已有章节信息
                result = contentService.updateSection(section);
            }else{
                //添加新章节
                result = contentService.saveSection(section);
            }
            //5.响应结果
            resp.getWriter().print(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //修改章节的状态
    public void updateSectionStatus(HttpServletRequest request,HttpServletResponse response){
        try {
            //获取参数
            int id = Integer.parseInt(request.getParameter("id"));
            int status = Integer.parseInt(request.getParameter("status"));

            //业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            String result = contentService.updateSectionStatus(id,status);

            //返回响应结果
            response.getWriter().println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //新增或修改课时信息
    public void saveOrUpdateLesson(HttpServletRequest request,HttpServletResponse response){
        try {
            //1.获取Dao接口设置的域对象中的参数
            Map<String,Object> map = (Map) request.getAttribute("map");
            //2.创建Course_Lesson
            Course_Lesson lesson = new Course_Lesson();
            //3.使用BeabUtils工具类，将map中的数据封装到lesson中
            BeanUtils.populate(lesson,map);
            //4.业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            //判断是修改或是新增章节信息
            String result;
            if(lesson.getId() != 0){
                //修改已有章节信息
                result = contentService.updateLesson(lesson);
            }else{
                //添加新章节
                result = contentService.saveLesson(lesson);
            }
            //5.响应结果
            response.getWriter().print(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

